<?php
include_once "settings.php";
include_once "messages.php";

function show_page() {
  global $data, $path, $style, $session_id, $header, $_POST;

  if (!isset($data["type"])) {
    header("Location: index.php?trg=main&sid=".$session_id);
    die();
  }

  $filename = $path[$style]["tpl"]."/importexport.htm";
  $fp = fopen($filename, "r");
  $contents = fread($fp, filesize ($filename));
  fclose ($fp);

//  preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);
//  $tmp_cont = str_replace("%MDL_NAME%", $mdl["name"], $matches[1]);
//  $contents = str_replace($matches[0], $rep_cont, $contents);


  $contents = str_replace("%ROOT_CSS%", $path[$style]["css"], $contents);
  $contents = str_replace("%ROOT_IMG%", $path[$style]["img"], $contents);
  $contents = str_replace("%LOGOUT%", getMessage("Logout"), $contents);
  $contents = str_replace("%MAINMENU%", getMessage("Main menu"), $contents);

  $contents = str_replace("%IMPORTEXPORT%", getMessage("Import / Export"), $contents);
  $contents = str_replace("%DAML%", getMessage("DAML"), $contents);
  $contents = str_replace("%KIF%", getMessage("KIF"), $contents);
  $contents = str_replace("%URL:%", getMessage("URL:"), $contents);

  if (isset($_POST["OntologyFile"])){
    $OntologyFile = $_POST["OntologyFile"];
    if (file_exists($OntologyFile)){
      $path_parts = pathinfo($OntologyFile);
      $com_object = true;
      if ($_POST["format"] == "1"){
        if (($path_parts["extension"] != "daml")&&($path_parts["extension"] != "DAML")){
          $contents = str_replace("%MESSAGETEXT%", getMessage("Invalid Extension"), $contents);
          $com_object = false;
        };
      }else{
        if (($path_parts["extension"] != "kif")&&($path_parts["extension"] != "KIF")){
          $contents = str_replace("%MESSAGETEXT%", getMessage("Invalid Extension"), $contents);
          $com_object = false;
        };
      };
      if ($com_object){
        $Deso_App = new COM("DESO_F.PME");
        $contents = str_replace("%MESSAGETEXT%", getMessage("Loading..."), $contents);
        if ( strlen($_SERVER["QUERY_STRING"]) != 0 ) {
           $out=$Deso_App->importontology($OntologyFile);
           if ($out=='0')
             $contents = str_replace("Loading...", getMessage("Import was Succesfuly Completed"), $contents);
            else
             $contents = str_replace("Loading...", getMessage("Import / Export Error"), $contents);
           $Deso_App->Destroy();
           unset( $Deso_App );
        }else {
        print("Gimme a command");
        }
      };
      $contents = str_replace("%MESSAGETEXT%", "", $contents);
    }else
      $contents = str_replace("%MESSAGETEXT%", getMessage("File not Found"), $contents);
  }else
    $contents = str_replace("%MESSAGETEXT%", "", $contents);
    
  $contents = str_replace("%SID%", $session_id, $contents);

  header ($header["html"]);
  header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    // Date in the past
  header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
  header ("Cache-Control: no-cache, must-revalidate");  // HTTP/1.1
  header ("Pragma: no-cache");                          // HTTP/1.0

  print ($contents);
}
?>